<script lang="ts" setup>
import { Dropdown, Menu } from 'ant-design-vue'
import type { MenuInfo } from 'ant-design-vue/lib/menu/src/interface'
import { computed } from 'vue'
import { useUserStore } from '@/store/modules/employee'
import { useDesign } from '@/hooks/web/useDesign'
import { useGo } from '@/hooks/web/usePage'
import { createAsyncComponent } from '@/utils/factory/createAsyncComponent'

defineOptions({ name: 'UserDropdown' })

const go = useGo()

type MenuEvent = 'profile' | 'logout' | 'doc' | 'lock'

const MenuItem = createAsyncComponent(() => import('./DropMenuItem.vue'))

const { prefixCls } = useDesign('header-user-dropdown')
const userStore = useUserStore()

const getUserInfo = computed(() => {
  const { realName = '' } = userStore.getUserInfo || {}
  return { realName }
})

//  login out
function handleLogOut() {
  userStore.confirmLoginOut()
}

function openProfile() {
  go('/profile/index')
}

function handleMenuClick(e: MenuInfo) {
  switch (e.key as MenuEvent) {
    case 'profile':
      openProfile()
      break
    case 'logout':
      handleLogOut()
      break
  }
}
</script>

<template>
  <Dropdown placement="bottom" :overlay-class-name="`${prefixCls}-dropdown-overlay`">
    <span :class="[prefixCls, `${prefixCls}--light`]" class="flex">
      <span :class="`${prefixCls}__info hidden md:block`">
        <span :class="`${prefixCls}__name  `" class="truncate">
          {{ getUserInfo.realName }}
        </span>
      </span>
    </span>

    <template #overlay>
      <Menu @click="handleMenuClick">
        <MenuItem key="profile" text="个人中心" icon="ion:person-outline" />
        <MenuItem key="logout" text="退出系统" icon="ion:power-outline" />
      </Menu>
    </template>
  </Dropdown>
</template>

<style lang="less">
@prefix-cls: ~'@{namespace}-header-user-dropdown';

.@{prefix-cls} {
  align-items: center;
  height: @header-height;
  padding: 0 0 0 10px;
  padding-right: 10px;
  overflow: hidden;
  font-size: 12px;
  cursor: pointer;

  &__header {
    border-radius: 50%;
  }

  &__name {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 90px;
    font-size: 14px;
  }

  &--dark {
    &:hover {
      background-color: @header-dark-bg-hover-color;
    }
  }

  &--light {
    &:hover {
      background-color: @header-light-bg-hover-color;
    }

    .@{prefix-cls}__desc {
      color: @header-light-desc-color;
    }
  }

  &-dropdown-overlay {
    .ant-dropdown-menu-item {
      min-width: 80px;
    }
  }
}
</style>
